import Vue from 'vue'
Vue.directive('drag', {
  // 指令的定义
  bind: function (el) {
    const odiv = el // 获取当前元素
    odiv.style.position = 'absolute'
    odiv.onmousedown = (e) => {
      // 算出鼠标相对元素的位置
      const disX = e.clientX - odiv.offsetLeft
      const disY = e.clientY - odiv.offsetTop
      document.onmousemove = (e) => {
        // 用鼠标的位置减去鼠标相对元素的位置，得到元素的位置
        const left = e.clientX - disX
        const top = e.clientY - disY
        // 移动当前元素
        odiv.style.left = left + 'px'
        odiv.style.top = top + 'px'
      }
      document.onmouseup = (e) => {
        document.onmousemove = null
        document.onmouseup = null
      }
    }
  }
})

export function range (num, min, max) {
  return Math.min(Math.max(num, min), max)
}

export function getDistance (touches) {
  return Math.sqrt(
    Math.abs(
      (touches[0].clientX - touches[1].clientX) *
              (touches[0].clientY - touches[1].clientY)
    )
  )
}
